package com.lipiao.smarthome.fragments;

import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.lipiao.smarthome.R;
import com.lipiao.smarthome.Utils.FurnitureStatusBean;
import com.lipiao.smarthome.Utils.InforUtil;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.IOException;

import okhttp3.FormBody;
import okhttp3.OkHttpClient;
import okhttp3.Request;
import okhttp3.RequestBody;
import okhttp3.Response;

public class StatusFragment extends Fragment {
    View rootView;
    public static TextView light_status;
    public static TextView air_status;
    public static TextView air_temp;
    public static TextView air_make_cold;
    public static TextView air_make_hot;
    public static TextView water_cold;
    public static TextView water_hot;
    public static TextView curtain_status;
    public static TextView sing_status;
    public static TextView tv_status;
    public static TextView tv_chan;
    public static TextView socket_status;
    public static TextView projector_status;
    private SwipeRefreshLayout swiperRefresh;
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        rootView = inflater.inflate(R.layout.fragment_status, container, false);
        light_status=rootView.findViewById(R.id.light_status);
        air_status=rootView.findViewById(R.id.air_status);
        air_temp=rootView.findViewById(R.id.air_temp);
        air_make_cold=rootView.findViewById(R.id.air_make_cold);
        air_make_hot=rootView.findViewById(R.id.air_make_hot);
        water_cold=rootView.findViewById(R.id.water_cold);
        water_hot=rootView.findViewById(R.id.water_hot);
        curtain_status=rootView.findViewById(R.id.curtain_status);
        sing_status=rootView.findViewById(R.id.sing_status);
        tv_status=rootView.findViewById(R.id.tv_status);
        tv_chan=rootView.findViewById(R.id.tv_chan);
        socket_status=rootView.findViewById(R.id.socket_status);
        projector_status=rootView.findViewById(R.id.projector_status);
        swiperRefresh=rootView.findViewById(R.id.swipe_refresh);
        initView();
        return rootView;
    }
    //    "light_status":"开",
//     "air_status":"开",
//     "air_temp":"50",
//     "air_make_cold":"24",
//     "air_make_hot":"关"
//    "water_cold":"10",
//     "water_hot":"关",
//     "curtain_status":"关",
//     "sing_status":"开",
//     "tv_status":"开"
//    "tv_chan":"金鹰频道",
//     "socket_status":"通电",
//     "projector_status":"开"
    private void initView() {
        swiperRefresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                refreshFruits();
            }
        });

    }

    public void refreshFruits() {
        new Thread(new Runnable() {
            @Override
            public void run() {
                try {
                    InforUtil.getFurnitureStatusBean();
                    Thread.sleep(2000);
                }catch (InterruptedException e){
                    e.printStackTrace();
                }
                getActivity(). runOnUiThread(new Runnable() {
                    @Override
                    public void run() {
                        update();
                       swiperRefresh.setRefreshing(false);
                    }
                });

            }
        }).start();
    }

    public static void update() {
        light_status.setText(InforUtil.getFurnitureStatusBean().getLight_status());
        air_status.setText(InforUtil.getFurnitureStatusBean().getAir_status());
        air_temp.setText(InforUtil.getFurnitureStatusBean().getAir_temp());
        air_make_cold.setText(InforUtil.getFurnitureStatusBean().getAir_make_cold());
        air_make_hot.setText(InforUtil.getFurnitureStatusBean().getAir_make_hot());
        water_cold.setText(InforUtil.getFurnitureStatusBean().getWater_cold());
        water_hot.setText(InforUtil.getFurnitureStatusBean().getWater_hot());
        //curtain_status.setText(InforUtil.getFurnitureStatusBean().getAir_status());
        sing_status.setText(InforUtil.getFurnitureStatusBean().getSing_status());
        tv_status.setText(InforUtil.getFurnitureStatusBean().getTv_status());
        tv_chan.setText(InforUtil.getFurnitureStatusBean().getTv_chan());
        socket_status.setText(InforUtil.getFurnitureStatusBean().getSocket_status());
        projector_status.setText(InforUtil.getFurnitureStatusBean().getProjector_status());
    }


    private void getjson(String message) {
        try {
            light_status.setText(message);
            JSONObject jsonObject = new JSONObject(message);
            String light= jsonObject.getString("tv_chan");
            String cold= jsonObject.getString("air_make_hot");
            light_status.setText(light);
            water_cold.setText(cold);
        } catch (JSONException e) {
            e.printStackTrace();
        }
    }
}
